package com.mycar2w.cloud.service.repository;

import com.mycar2w.cloud.entity.ApplieOrder;
import com.mycar2w.cloud.entity.Car;
import com.mycar2w.cloud.service.constant.ConstantInfo;
import com.umuw.cloud.core.repository.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * created time 2017/4/4
 *
 * @author ljc
 * @version V1.0
 */
@Repository
public interface CarRepository extends BaseRepository<Car>{
    /**
     * 无条件查看汽车列表
     * @param pageIndex 页码
     * @return 汽车集合 Page
     */
    default Page<Car> getPageByNone(Integer pageIndex) {
        Pageable pageable = new PageRequest(pageIndex, ConstantInfo.PAGE_SIZE, new Sort(Sort.Direction.DESC,"id"));

        Specification<Car> spec = (root, query, cb) -> {
            Path<String> isSale=root.get("isSale");
            Predicate pFirst=cb.equal(isSale,"0");
            return pFirst;
        };
        return this.findAll(spec,pageable);
    }

    /**
     * 价格区间条件查看汽车列表
     * @param beginPrice 起始价格
     * @param endPrice 最终价格
     * @param pageIndex 页码
     * @param sort 升序还是降序
     * @return 汽车集合 Page
     */
    default Page<Car> getPageByBetweenPriceNo(String beginPrice, String endPrice, Integer pageIndex,Integer sort){
        Sort.Direction sortRule;
        if (sort==0){
            sortRule=Sort.Direction.DESC;
        }else {
            sortRule=Sort.Direction.ASC;
        }
        Pageable pageable = new PageRequest(pageIndex,ConstantInfo.PAGE_SIZE,new Sort(sortRule,"id"));

        Specification<Car> spec=(root,query,cb)->{
            Path<Double> saleMoney=root.get("saleMoney");
            Path<String> sbsSale=root.get("isSale");
            Predicate pFirst=cb.between(saleMoney,Double.parseDouble(beginPrice),Double.parseDouble(endPrice));
            Predicate pSecond=cb.equal(sbsSale,"0");
            Predicate pThird=cb.and(pFirst,pSecond);
            return pThird;
        };
        return this.findAll(spec,pageable);


    }

    /**
     * 地区查找汽车
     * @param tUserIdList 地址用户Id
     * @param pageIndex 页码
     * @param sort 排序规则
     * @return 汽车集合 Page
     */
    default Page<Car> getPageByArea(List<Long> tUserIdList, Integer pageIndex, Integer sort){
        Sort.Direction sortRule;
        if (sort==0){
            sortRule=Sort.Direction.DESC;
        }else {
            sortRule=Sort.Direction.ASC;
        }
        Pageable pageable = new PageRequest(pageIndex,ConstantInfo.PAGE_SIZE,new Sort(sortRule,"id"));
        Specification<Car> spec=(root,query,cb)->{
            Path<Long> userId=root.get("userId");
            Path<String> isSale=root.get("isSale");
            Predicate pFirst=userId.in(tUserIdList);
            Predicate pSecond=cb.equal(isSale,"0");
            Predicate pThird=cb.and(pFirst,pSecond);
            return pThird;
        };
        return this.findAll(spec,pageable);
    }

    /**
     * 交易次数查找汽车
     * @param count 交易次数
     * @param pageIndex 页码
     * @param sort 排序规则
     * @return 汽车集合 Page
     */
    default Page<Car> getPageByTransferCount(String count, Integer pageIndex, Integer sort){
        Sort.Direction sortRule;
        if (sort==0){
            sortRule=Sort.Direction.DESC;
        }else {
            sortRule=Sort.Direction.ASC;
        }
        Pageable pageable = new PageRequest(pageIndex,ConstantInfo.PAGE_SIZE,new Sort(sortRule,"id"));
        Specification<Car> spec=(root,query,cb)->{
            Path<String> transCount=root.get("transferCount");
            Path<String> isSale=root.get("isSale");
            Predicate pFirst=cb.equal(transCount,count);
            Predicate pSecond=cb.equal(isSale,"0");
            Predicate pThird=cb.and(pFirst,pSecond);
            return pThird;
        };
        return this.findAll(spec,pageable);
    }

    List<Car> findByUserId(Long id);

    Car findById(Long carId);


    default List<Car> getPageFromApplieOrders(List<ApplieOrder> orderList){
        Specification<Car> spec=(root,query,cb)->{
            Path<ApplieOrder> applieOrder=root.get("appliedOrderId");
           Predicate pre=applieOrder.in(orderList);
            return pre;
        };
        return this.findAll(spec);
    }

    Car findByAppliedOrderId(ApplieOrder applieOrderId);
}
